class GeocodeRequest {
  final String key;
  final String location;

  GeocodeRequest({
    required this.key,
    required this.location,
  });

  Map<String, dynamic> toJson() => {
        'key': key,
        'location': location,
      };

  // 将请求参数转换成 URL 编码的字符串
  String toUrlEncodedString() {
    return Uri(queryParameters: toJson()).query;
  }
}

//返回值
class Address {
  final String country;
  final String province;
  final String city;
  final String district;
  final String township;
  final String neighborhood;
  final String street;
  final String number;

  Address({
    required this.country,
    required this.province,
    required this.city,
    required this.district,
    required this.township,
    required this.neighborhood,
    required this.street,
    required this.number,
  });

  factory Address.fromJson(Map<String, dynamic> json) {
    final addressComponent = json['addressComponent'] as Map<String, dynamic>;
    return Address(
      country: addressComponent['country'],
      province: addressComponent['province'],
      city: addressComponent['city'],
      district: addressComponent['district'],
      township: addressComponent['township'] ?? '',
      neighborhood: (addressComponent['neighborhood']
              as Map<String, dynamic>?)?['name'] ??
          '',
      street: (addressComponent['streetNumber']
              as Map<String, dynamic>?)?['street'] ??
          '',
      number: (addressComponent['streetNumber']
              as Map<String, dynamic>?)?['number'] ??
          '',
    );
  }
}

//rsp部分
class AmapResponse {
  final int status;
  final String info;
  final Regeocode regeocode;

  AmapResponse(
      {required this.status, required this.info, required this.regeocode});

  factory AmapResponse.fromJson(Map<String, dynamic> json) {
    return AmapResponse(
        status: json['status'],
        info: json['info'],
        regeocode: Regeocode.fromJson(json['regeocode']));
  }
}

class Regeocode {
  final Address addressComponent;
  final String formattedAddress;

  Regeocode({required this.addressComponent, required this.formattedAddress});

  factory Regeocode.fromJson(Map<String, dynamic> json) {
    return Regeocode(
        addressComponent: Address.fromJson(json['addressComponent']),
        formattedAddress: json['formattedAddress']);
  }
}

//周边搜索返回值
class PlaceSearchResponse {
  final int count;
  final String infocode;
  final List<Place> pois;
  final int status;
  final String info;

  PlaceSearchResponse(
      {required this.count,
      required this.infocode,
      required this.pois,
      required this.status,
      required this.info});

  factory PlaceSearchResponse.fromJson(Map<String, dynamic> json) {
    return PlaceSearchResponse(
        count: json['count'],
        infocode: json['infocode'],
        status: json['status'],
        info: json['info'],
        pois: json['pois'].map((e) => Place.fromJson(e)).toList());
  }
}

class Place {
  final String name;
  final String address;

  Place({required this.name, required this.address});

  factory Place.fromJson(Map<String, dynamic> json) {
    return Place(name: json['name'], address: json['address']);
  }
}
